#============================================================
#    File Utils
#============================================================
# - datetime: 2022-08-23 18:26:26
#============================================================

##  文件工具类

class Scan:
	enum {
		DIRECTORY,
		FILE,
	}
	
	static func method(path: String, list: Array, recursive:bool, type):
		var directory := DirAccess.open(path)
		if directory == null:
			printerr("err: ", path)
			return
		directory.list_dir_begin()
		# 遍历文件
		var dir_list := []
		var file_list := []
		var file := ""
		file = directory.get_next()
		while file != "":
			# 目录
			if directory.current_is_dir() and not file.begins_with("."):
				dir_list.append( path.path_join(file) )
			# 文件
			elif not directory.current_is_dir() and not file.begins_with("."):
				file_list.append( path.path_join(file) )
			
			file = directory.get_next()
		# 添加
		if type == DIRECTORY:
			list.append_array(dir_list)
		else:
			list.append_array(file_list)
		# 递归扫描
		if recursive:
			for dir in dir_list:
				method(dir, list, recursive, type)


##  文件是否存在
##[br]
##[br][code]file_path[/code]  文件路径
static func file_exists(file_path: String) -> bool:
	return FileAccess.file_exists(file_path)


##  目录是否存在
##[br]
##[br][code]dir_path[/code]  目录所在路径
static func dir_exists(dir_path: String) -> bool:
	return DirAccess.dir_exists_absolute(dir_path)


##  保存为文本文件
##[br]
##[br][code]file_path[/code]  文件路径
##[br][code]txt[/code]  文本内容
##[br]
##[br][code]return[/code]  返回 [code] ERROR [/code] 数据
static func write_as_text(file_path:String, text:String) -> int:
	var file := FileAccess.open(file_path, FileAccess.WRITE)
	if file:
		file.store_string(text)
		file = null
	return -1


##  读取文件
##[br]
##[br][code]file_path[/code]  文件路径
##[br][code]skip_cr[/code]  跳过 \r CR 字符
## If skip_cr is true, carriage return characters (\r, CR) will be ignored when parsing the UTF-8, so that only line feed characters (\n, LF) represent a new line (Unix convention).
static func read_as_text(file_path: String, skip_cr: bool = false):
	if FileAccess.file_exists(file_path):
		var file := FileAccess.open(file_path, FileAccess.WRITE)
		if file:
			var text = file.get_as_text(skip_cr)
			file = null
			return text
	return ""


##  保存为变量数据
##[br]
##[br][code]file_path[/code]  文件路径
##[br][code]data[/code]  数据
##[br][code]full_objects[/code]  如果是 true，则允许编码对象（并且可能包含代码）。
static func write_as_var(file_path: String, data, full_objects: bool = false) -> int:
	var file := FileAccess.open(file_path, FileAccess.WRITE)
	if file:
		file.store_var(data, full_objects)
		file = null
	return -1


##  读取 var 数据
static func read_as_var(file_path: String, allow_objects: bool = false):
	if FileAccess.file_exists(file_path):
		var file := FileAccess.open(file_path, FileAccess.WRITE)
		if file:
			var data = file.get_var(allow_objects)
			file = null
			return data


#  扫描目录
static func scan_directory(path: String, recursive:= false) -> Array[String]:
	assert(dir_exists(path), "没有这个路径")
	var list : Array[String] = []
	Scan.method(path, list, recursive, Scan.DIRECTORY)
	return list


#  扫描文件
static func scan_file(path: String, recursive:= false) -> Array[String]:
	assert(dir_exists(path), "没有这个路径")
	var list : Array[String] = []
	Scan.method(path, list, recursive, Scan.FILE)
	return list


